package com.example.qqfake;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.qqfake.User;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegisterActivity extends AppCompatActivity {
    public static final int RESULT_CODE_REGISTER=0;
    private Button btn_register;
    private EditText et_register_username,et_register_password,et_again_password;
    private Client1 client1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //注册按钮
        btn_register= findViewById(R.id.btn_register);
        //用户名编辑框
        et_register_username= findViewById(R.id.et_register_username);
        //密码编辑框
        et_register_password=findViewById(R.id.et_register_password);
        //再次输入密码编辑框
        et_again_password=findViewById(R.id.et_again_password);


        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取三个编辑框的内容
                String et_name=et_register_username.getText().toString();
                String et_password=et_register_password.getText().toString();
                String md5String = MD5.getMD5(et_password);

                String et_confirm=et_again_password.getText().toString();
                //判断异常情况弹窗
                //子线程开启socket
                new Thread() {
                    private BufferedReader bufferedReader = null;
                    public void run() {
                        //发送socket
                        try {
                            client1 = new Client1();
                            String content1 = null;
                            bufferedReader = new BufferedReader(new InputStreamReader(client1.socket.getInputStream()));

                            Send(et_name+" "+md5String);
                            Log.v("msg","sendgood");

                            while((content1 = bufferedReader .readLine()) != null) {
                                String finalContent = content1;
                                Log.v("msg",content1);
                                if(!isFinishing()){
                                    runOnUiThread(new Runnable() {//此run方法将由主线程运行
                                        @Override
                                        public void run() {
                                            //编辑框为空
                                            if(TextUtils.isEmpty(et_name)){
                                                Toast.makeText(RegisterActivity.this,"账号不能为空！",Toast.LENGTH_SHORT).show();
                                                //对账号进行正则化验证，调用下面写的idTelPhoneNumber方法
                                            }else if(!isTelPhoneNumber(et_name)){
                                                Toast.makeText(RegisterActivity.this,"请输入正确的账号！",Toast.LENGTH_SHORT).show();
                                            } else if(TextUtils.isEmpty(et_password)){
                                                Toast.makeText(RegisterActivity.this,"密码不能为空！",Toast.LENGTH_SHORT).show();
                                                //两次密码框内容不一致
                                            }else if(!TextUtils.equals(et_password,et_confirm)){
                                                Toast.makeText(RegisterActivity.this,"密码不一致！",Toast.LENGTH_SHORT).show();
                                            } else if (finalContent.equals("false2")) {
                                                Toast.makeText(RegisterActivity.this, "该账号已注册！", Toast.LENGTH_SHORT).show();
                                            } else{
                                                Toast.makeText(RegisterActivity.this,"注册成功！",Toast.LENGTH_SHORT).show();

                                                RegisterActivity.this.finish();
                                            }

                                        }
                                    });
                                }
                                Log.v("msg","jieshou");
                            }

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();

            }
        });
    }
    /*正则化验证账号方法*/
    public static boolean isTelPhoneNumber(String mobile) {
        if (mobile != null) {
            Pattern pattern = Pattern.compile("^\\d+$");
            Matcher matcher = pattern.matcher(mobile);
            return matcher.matches();
        }else{
            return false;
        }
    }
    //发送登录验证信息
    public void Send(String content){
        try {
            //发送消息
            client1.send(String.valueOf(1),String.valueOf(1),content);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //重写onDestroy()方法
    @Override
    protected void onDestroy() {
        super.onDestroy();
        try{
            //关闭输入流，socket连接

            if(client1.socket!=null){
                client1.socket.close();
            }
        }catch(IOException e){
            e.printStackTrace();

        }

    }
}
